package com.example.listapp;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyPerformanceArrayAdapter extends ArrayAdapter<String> {

	private final Activity context;
	private final String[] names;
	
	static class ViewHolder {
		public TextView textView;
		public ImageView imageView;
	}
	
	public MyPerformanceArrayAdapter(Activity context, String[] names) {
		super(context, R.layout.rowlayout, names);
		this.context = context;
		this.names = names;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;
		if(rowView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.rowlayout, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.textView = (TextView) rowView.findViewById(R.id.TextView01);
			viewHolder.imageView = (ImageView) rowView.findViewById(R.id.ImageView01);
			rowView.setTag(viewHolder);
		}
		
		ViewHolder holder = (ViewHolder) rowView.getTag();
		String s = names[position];
		holder.textView.setText(s);
		if(s.startsWith("Windows7") || s.startsWith("iPhone") || s.startsWith("Solaris")) {
			holder.imageView.setImageResource(R.drawable.apple);
		} else {
			holder.imageView.setImageResource(R.drawable.bannana);
		}
		
		return rowView;
	}
}
